import gtk
import gobject
import pango
import pygtk

from lib.w.widget.base import Base

class TreeView(Base):

	tipo_dict = {
		'bool': gobject.TYPE_BOOLEAN,
		'char': gobject.TYPE_CHAR,
		'double': gobject.TYPE_DOUBLE,
		'float': gobject.TYPE_FLOAT,
		'int': gobject.TYPE_INT,
		'long': gobject.TYPE_LONG,
		'none': gobject.TYPE_NONE,
		'imagem': gtk.gdk.Pixbuf,
		'string': gobject.TYPE_STRING,
		'data': gobject.TYPE_STRING,
	}

	def __init__(self, w):

		self.w = w

	def set_column(self, item_list):

			self.cols = item_list
			type_format = [gobject.TYPE_PYOBJECT]
			for item in item_list:
				tipo = item[2]
				type_format.append(self.tipo_dict.get(tipo, 'string'))
			
			self.model = gtk.ListStore(*type_format)
			self.w.set_model(self.model)

			self.toggle_dict = {}

			pos = 1
			for item in item_list:
				if len(item) == 6:
					nome_coluna, nome, tipo, alinhamento, largura, editavel = item
				else:
					nome_coluna, nome, tipo, alinhamento, largura, editavel, ttype = item

				if tipo == 'imagem':
					renderer = gtk.CellRendererPixbuf()
					renderer.set_property('stock_size', gtk.ICON_SIZE_SMALL_TOOLBAR)
				elif tipo == 'bool':
					renderer = gtk.CellRendererToggle()
					self.toggle_dict[renderer] = nome
					renderer.set_property('activatable', editavel)
					renderer.connect('toggled', self.on_toggle)
				else:
					renderer = gtk.CellRendererText()
					#renderer.set_property('weight', pango.WEIGHT_BOLD)
					renderer.set_property('editable', editavel)
					renderer.set_fixed_size(largura, -1)
					renderer.connect('edited', self.on_text_edited, pos)
				if tipo != 'bool':
					renderer.set_property('xalign', alinhamento)
					renderer.set_property('ypad', 1)
				#renderer.set_property('foreground', 'blue')
				if tipo == 'imagem':
					column = gtk.TreeViewColumn(self.encode(nome_coluna), renderer, pixbuf=pos)
				elif tipo == 'bool':
					column = gtk.TreeViewColumn(nome_coluna, renderer, active=pos)
				else:
					column = gtk.TreeViewColumn(self.encode(nome_coluna), renderer, text=pos)
				column.set_sort_column_id(pos)
				column.set_alignment(0.5)
				column.set_fixed_width(largura)
				column.set_reorderable(True)
				column.set_resizable(True)
				column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
				self.w.append_column(column)
				pos += 1

	def on_text_edited(self, cell, path, new_text, col):

		iter = self.model.get_iter(path)
		k = self.cols[col-1][1]
		ttype = self.cols[col-1][6]

		valor = ttype().parse(new_text)
		d = self.model.get_value(iter, 0)
		d[k] = valor
		self.model.set_value(iter, 0, d)
		self.model.set_value(iter, col, valor)
		
	def on_toggle(self, cell, path):

		valor = not self.model[path][1]
		self.model[path][1] = valor
		d = self.model.get_value(self.model[path].iter, 0)
		d[self.toggle_dict[cell]] = valor
		self.model.set_value(self.model[path].iter, 0, d)

	def clear(self):

		self.model.clear()
			
	def fill(self, item_list):

		self.model.clear()
		for d in item_list:
			self.append_dict(d)

	def append_dict(self, d):

		l = []
		for c in self.cols:
			t = d.get(c[1])
			if len(c) > 6:
				try:
					t = c[6]().format(t)
				except:
					pass
			l.append(t)
		
		self.append(l, d)

	def append(self, l, d):

		r = [d]
		for v in l:
			r.append(v)
		
		self.model.append(r)

	def get_dict_list(self):
		iter = self.model.get_iter_first()
		r = []
		while iter:
			d = self.model.get_value(iter, 0)
			r.append(d)
			iter = self.model.iter_next(iter)

		return r
	
	def get_selected_dict(self):

		ret = None
		sel = self.get_selection()
		if sel:
			l, i = sel.get_selected()
			if l and i:
				ret = self.model.get_value(i, 0)

		return ret

	def select_row(self, row, col=0, start_editing=False):

		c = self.w.get_column(col)
		self.w.set_cursor(row, c, start_editing)

	def __getattr__(self, attr):
		return getattr(self.w, attr)
